﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace BadHabit.Factory.Controls {
    /// <summary>
    /// Interaction logic for PaymentPanel.xaml
    /// </summary>
    public partial class PaymentPanel : GenericPanel {

        // this is a key for the TotalPrice property
        internal static readonly DependencyProperty TotalPriceProperty =
            DependencyProperty.Register("TotalPrice", typeof(double), typeof(PaymentPanel), null);

        // this is a key for the TotalPrice property
        internal static readonly DependencyProperty StillToPayProperty =
            DependencyProperty.Register("StillToPay", typeof(double), typeof(PaymentPanel), null);

        // this is a key for the TotalPrice property
        internal static readonly DependencyProperty InDebtProperty =
            DependencyProperty.Register("InDebt", typeof(double), typeof(PaymentPanel), null);

        // this is a key for the TotalPrice property
        internal static readonly DependencyProperty IsInDebtProperty =
            DependencyProperty.Register("IsInDebt", typeof(bool), typeof(PaymentPanel), null);

        public bool IsInDebt {
            get {
                return (bool)GetValue(IsInDebtProperty);
            }
            private set {
                SetValue(IsInDebtProperty, value);
            }
        }

        public double InDebt {
            get {
                return (double)GetValue(InDebtProperty);
            }
            private set {
                SetValue(InDebtProperty, value);
                IsInDebt = (value>0);
            }
        }

        public double StillToPay {
            get {
                return (double)GetValue(StillToPayProperty);
            }
            private set {
                SetValue(StillToPayProperty, value);
            }
        }

        public double TotalPrice {
            get {
                return (double)GetValue(TotalPriceProperty);
            }
            private set {
                SetValue(TotalPriceProperty, value);
            }
        }

        /// <summary>
        /// Default Constructor.
        /// </summary>
        public PaymentPanel() {
            InitializeComponent();
        }       

        private void UpdateBalanceAmount() {
            TotalPrice = CalcTotalPrice();
            StillToPay = CalcStillToPay();
            InDebt = CalcInDebt();
        }


        /// <summary>
        /// Calcs the total price.
        /// </summary>
        /// <returns></returns>
        private double CalcTotalPrice() {
            Data.ProjectDataSet.ProjectPaymentDataTable payments = DataContextAsProjectDataSet.ProjectPayment;
            if (payments == null)
                return 0;
            double price = 0;
            double.TryParse(payments.Compute("SUM(Value)", "").ToString(), out price);            
            return price;
        }

        /// <summary>
        /// Calcs the still to pay amount.
        /// </summary>
        /// <returns></returns>
        private double CalcStillToPay() {
            Data.ProjectDataSet.ProjectPaymentDataTable payments = DataContextAsProjectDataSet.ProjectPayment;
            if (payments == null)
                return 0;
            double payed = 0;
            double.TryParse(payments.Compute("SUM(PayedValue)", "").ToString(), out payed);
            return TotalPrice - payed;                
        }

        /// <summary>
        /// Calcs the in debt amount.
        /// </summary>
        /// <returns></returns>
        private double CalcInDebt() {
            Data.ProjectDataSet.ProjectPaymentDataTable payments = DataContextAsProjectDataSet.ProjectPayment;
            if (payments == null)
                return 0;
            double shouldPay = 0;
            double.TryParse(payments.Compute("SUM(Value)", "PlannedPayDate < '" + DateTime.Today + "'").ToString(), out shouldPay);            
            double payed = 0;
            double.TryParse(payments.Compute("SUM(PayedValue)", "").ToString(), out payed);
            return shouldPay - payed;

        }

      
        

        /// <summary>
        /// Set payment default values.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void paymentDataGrid_InitializingNewItem(object sender, Microsoft.Windows.Controls.InitializingNewItemEventArgs e) {
            Data.ProjectDataSet.ProjectPaymentRow payment = Helper.GetRowFromRowView<Data.ProjectDataSet.ProjectPaymentRow>(e.NewItem);
            payment.PaymentUId = Guid.NewGuid().ToString().ToLower();
            if(DataContextAsProjectDataSet.Proposal.Count>0)
                payment.ProposalUId = DataContextAsProjectDataSet.Proposal[0].ProposalUId;
            payment.ProjectUId = Project.ProjectUId;
            payment.PlannedPayDate = Project.EstimatedEndDate;
            payment.Value = 0;
            
        }

        
        private void paymentDataGrid_RowEditEnding(object sender, Microsoft.Windows.Controls.DataGridRowEditEndingEventArgs e) {
            UpdateBalanceAmount();
        }

        private void paymentPanel_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e) {
            UpdateBalanceAmount();
            System.Data.DataView view = DataContextAsProjectDataSet.ProjectPayment.DefaultView;
            view.Sort = "PlannedPayDate";
            paymentDataGrid.ItemsSource = view; 
        }

       





    }
}
